package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.base.entity.StaticAttrDO;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName : TransTxPlanHandler
 * @Description : 转移至的通信计划
 * @Author : chen.cheng110
 * @Date : 2020/12/23 16:00
 */
@Service
public class TransTxPlanHandler extends AbstractAttrHandler {
    @Resource
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        String transPlanTxSign = "";
        KiteAttr transPlanTx = KiteBeanUtils.transform(commonAttr, KiteAttr.class);

        List<KiteProdInstFunc> kiteProdInstFuncsTx = querySceneInstService.listProdInstFuncsByProdId(sceneInstId, LocalKeyConsts.FUNC_PROD_ID_TX);
        if (KiteListUtils.isEmpty(kiteProdInstFuncsTx)) {
            kiteProdInstFuncsTx = querySceneInstService.listProdInstFuncsByProdId(sceneInstId, LocalKeyConsts.FUNC_PROD_ID_NBTX);
        }
        if (KiteListUtils.isEmpty(kiteProdInstFuncsTx)) {
            kiteProdInstFuncsTx = querySceneInstService.listProdInstFuncsByProdId(sceneInstId, LocalKeyConsts.FUNC_PROD_ID_5GTX);
        }
        if (KiteListUtils.isEmpty(kiteProdInstFuncsTx)) {
            kiteProdInstFuncsTx = querySceneInstService.listProdInstFuncsByProdId(sceneInstId, LocalKeyConsts.FUNC_PROD_ID_YF5GTX);
        }

        List<KiteAttrValue> attrValuesTx = new ArrayList<>();
        List<StaticAttrDO> attrValues = new ArrayList<>();
        if (KiteListUtils.isNotEmpty(kiteProdInstFuncsTx) && Objects.nonNull(transPlanTx)) {
            for (KiteProdInstFunc funcProdInst : kiteProdInstFuncsTx) {
                Map<String, KiteAttr> newsFunctionAttrMap = funcProdInst.getAttrMap();
                // 200004128 通信计划标识
                transPlanTxSign = newsFunctionAttrMap.get("200004128") == null ? "" : newsFunctionAttrMap.get("200004128").getValueDesc();

                KiteAttrValue attrValue = new KiteAttrValue();
                attrValue.setAttrValue(transPlanTxSign);
                attrValue.setAttrValueName(transPlanTxSign);
                attrValuesTx.add(attrValue);

                StaticAttrDO kiteAttrValue = new StaticAttrDO();
                kiteAttrValue.setAttrValue(transPlanTxSign);
                kiteAttrValue.setAttrValueDesc(transPlanTxSign);
                attrValues.add(kiteAttrValue);
            }
            String codea = "";
            transPlanTx.setAttrValues(attrValuesTx);
            codea = DcPublicCache.getCodea("20201223002", transPlanTx.getFieldName());
            if (KiteStringUtils.isNotEmpty(codea)) {
                transPlanTx.setModuleId(codea);
            }
            SpringUtil.getBean(IOperateSceneCommonService.class).setCommonAttr(sceneInstId, transPlanTx);
            commonAttr.setAttrValues(attrValues);
        }
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        //变更业务的场景初始化过程中不校验
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(sceneInst.getServiceOfferId())
            && KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(sceneInst.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE_SCENE))) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        String transPlanTxSign = "";
        KiteAttr transPlanTx = querySceneInstService.getCommonAttr(sceneInstId, fieldName);

        List<KiteProdInstFunc> kiteProdInstFuncsTx = querySceneInstService.listProdInstFuncsByProdId(sceneInstId, LocalKeyConsts.FUNC_PROD_ID_TX);
        if (KiteListUtils.isEmpty(kiteProdInstFuncsTx)) {
            kiteProdInstFuncsTx = querySceneInstService.listProdInstFuncsByProdId(sceneInstId, LocalKeyConsts.FUNC_PROD_ID_NBTX);
        }
        if (KiteListUtils.isEmpty(kiteProdInstFuncsTx)) {
            kiteProdInstFuncsTx = querySceneInstService.listProdInstFuncsByProdId(sceneInstId, LocalKeyConsts.FUNC_PROD_ID_5GTX);
        }
        if (KiteListUtils.isEmpty(kiteProdInstFuncsTx)) {
            kiteProdInstFuncsTx = querySceneInstService.listProdInstFuncsByProdId(sceneInstId, LocalKeyConsts.FUNC_PROD_ID_YF5GTX);
        }

        List<KiteAttrValue> attrValuesTx = new ArrayList<>();
        if (KiteListUtils.isNotEmpty(kiteProdInstFuncsTx) && Objects.nonNull(transPlanTx)) {
            for (KiteProdInstFunc funcProdInst : kiteProdInstFuncsTx) {
                Map<String, KiteAttr> newsFunctionAttrMap = funcProdInst.getAttrMap();
                // 200004128 通信计划标识
                transPlanTxSign = newsFunctionAttrMap.get("200004128") == null ? "" : newsFunctionAttrMap.get("200004128").getValueDesc();
                KiteAttrValue kiteAttrValue = new KiteAttrValue();
                kiteAttrValue.setAttrValue(transPlanTxSign);
                kiteAttrValue.setAttrValueName(transPlanTxSign);
                attrValuesTx.add(kiteAttrValue);
            }
            transPlanTx.setAttrValues(attrValuesTx);
            operateSceneCommonService.setCommonAttr(sceneInstId, transPlanTx);
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }

}
